//
//  APLibs_APTagView_ExampleVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/3/8.
//

import UIKit

class APLibs_APTagView_ExampleVC: UIViewController {

    private var tagView: APTagView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = .lightGray
        creatUI()
        
        let attStr = NSMutableAttributedString()
        let sub1 = NSMutableAttributedString(string: "1_王者荣耀")
        sub1.addAttributes([.font: UIFont.systemFont(ofSize: 14)], range: NSRange(location: 0, length: sub1.length))
        sub1.addAttributes([.foregroundColor: UIColor.blue], range: NSRange(location: 0, length: sub1.length))
        attStr.append(sub1)
        let sub2 = NSMutableAttributedString(string: "0")
        sub2.addAttributes([.font: UIFont.systemFont(ofSize: 18)], range: NSRange(location: 0, length: sub2.length))
        sub2.addAttributes([.foregroundColor: UIColor.purple], range: NSRange(location: 0, length: sub2.length))
        attStr.append(sub2)
        
        let currentTagW = attStr.boundingRect(with: CGSize(width: .max, height: 50), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).size.width + 1.0
        
        print("currentTagW is \(currentTagW)")
        
        let currentTagW1 = "1_王者荣耀0".boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: 50), options: .usesLineFragmentOrigin, attributes: [.font: UIFont.systemFont(ofSize: 12)], context: nil).size.width + 1.0
        print("currentTagW1 is \(currentTagW1)")
        
        let bt = UIButton(type: .custom)
        view.addSubview(bt)
        bt.setAttributedTitle(attStr, for: .normal)
        bt.sizeToFit()
        bt.frame = CGRect(x: 20, y: 400, width: bt.bounds.size.width, height: bt.bounds.size.height)
        bt.backgroundColor = .purple
        print("bt size \(bt.bounds.size.width)")

    }

    private func creatUI() {
        ///配置假数据
        var tagModels: [APTagModelProtocol] = []
        for index in 0...10 {
            
            let tagModel = APTagModel()

            tagModel.id = "\(index)"
            tagModel.text = "\(index)" + "_王者荣耀"

            tagModels.append(tagModel)
        }
        let configuration = APTagConfiguration.defaultConfiguration()
        let tagViewHeight = APTagView.height(with: configuration, tagModels: tagModels)
        tagView = APTagView(frame: CGRect(x: 16, y: 100, width: UIScreen.main.bounds.size.width - 32, height: tagViewHeight))
        view.addSubview(tagView)
        tagView.setup(configuration: configuration, tagModels: tagModels)
        tagView.backgroundColor = .white
    }
}
